
Date.prototype.toLocaleString = function() {
  // 补0   例如 2018/7/10 14:7:2  补完后为 2018/07/10 14:07:02
  function addZero(num) {
      if(num<10)
          return "0" + num;
      return num;
  }
  // 按自定义拼接格式返回
  return addZero(this.getMonth() + 1) + "/" + addZero(this.getDate()) + "/" +
      addZero(this.getHours()) + ":" + addZero(this.getMinutes()) 
};

Page({
      data :{

        msgListData:[],

      },


      //点击列表中的消息即可跳转到单人聊天
      navigaToChatRoom: function(e){
        wx.navigateTo({
          // url: '/pages/chatRoom/chatRoom?nickname='+this.data.counterInfo.nickName +'&openid='+this.data.counterInfo.openid
          url:'/pages/chatRoom/chatRoom?openid='+JSON.stringify(e.currentTarget.id)
        })
        console.log("跳转至与"+e.currentTarget.id+"的聊天页面")
    },

    //对列表内时间进行排序
    sortArray: function(List){
      for(var i = 1 ;i<List.length;i++){
        for(var y =0 ;y<List.length-1;y++){
          if(List[y].time<List[y+1].time){
            var obj=List[y]
            List[y]=List[y+1]
            List[y+1]=obj
          }
        }
      }
    },
    



 
      //渲染时获取缓存的MsgListData若为空则设置MsgListData缓存
      onShow(){
         var msgListData = wx.getStorageSync('msgListData')
         console.log(msgListData)
         if (msgListData!=null){

          this.sortArray(msgListData)
          for(var i = 0 ;i<msgListData.length;i++){
            msgListData[i].time=new Date(msgListData[i].time).toLocaleString()
          }
          this.setData({
            msgListData:msgListData
          })

          console.log("缓存Msg成功")
         
        }
         else{
           wx.setStorageSync('msgListData',[])
           console.log("msg为空设置缓存")
         }
        
      },

      
onload(){
  setInterval(res=>{
    if(that.data.msgListData!==wx.getStorageSync('msgListData'))
    that.setData({
      msgListData:wx.getStorageSync('msgListData'),
    })
  },3000)
}


});




// for (let i = 0; i < this.data.msgListData.length; i++) {
//   if (this.data.msgListData[i].openid=="openid1"){
//     this.data.msgListData[i].recentmessage="by order of the peaky blinders"
//     this.setData({
//       msgListData:this.data.msgListData
//     })
//   }
//       }